package com.sk.waste.app.bases

import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.View.OnClickListener
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.Button
import android.widget.EditText
import android.widget.ImageButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.lc.smc.lib.activitys.LCActivity
import com.lc.smc.lib.utils.ProgressUtil
import com.sk.waste.app.R
import com.sk.waste.app.utils.LcProgressUtil
import org.greenrobot.eventbus.EventBus


open class BaseActiivity : LCActivity() {

    lateinit var acPresenter: BasePresenter
    var progressDialog: LcProgressUtil? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        binddingView()
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
    }

    override fun getExras() {
    }

    override fun findView() {
    }

    override fun setListener() {
    }

    override fun initPresenter() {
    }

    protected open fun binddingView() {

    }

    open fun setBack() {
        val bttBack: ImageButton = findViewById(R.id.view_top_back)
        if (bttBack != null) {
            bttBack.setOnClickListener { finish() }
        }
    }

    open fun setTitleView(title: String) {
        val ttvTitle: TextView = findViewById(R.id.view_top_title)
        if (ttvTitle != null) {
            ttvTitle.text = title
        }

    }

    open fun setSubmit(text: String, onClickListener: OnClickListener?) {
        val bttBack: Button = findViewById(R.id.view_top_submit)
        if (bttBack != null) {
            if (TextUtils.isEmpty(text)) {
                bttBack.visibility = View.GONE
            }
            bttBack.text = text
            bttBack.setOnClickListener { onClickListener?.onClick(it) }
        }
    }

    fun setStatusBarColor(resId: Int) {
        val window = window
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = ContextCompat.getColor(this, resId)
    }

    /**
     *  @time 2025/5/6  19:48
     *  @author Im_jingwei
     *  @desc 设置状态栏字体颜色
     *  @param true 黑色，false 白色
     */
    fun setTitleColorLight(boolean: Boolean) {
        WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars = boolean;
    }

   open fun showProgress() {
        if (progressDialog == null) {
            progressDialog = LcProgressUtil(this)
        }
        progressDialog!!.show()
    }

    open fun showProgress(msg:String) {
        if (progressDialog == null) {
            progressDialog = LcProgressUtil(this)
        }
        progressDialog!!.setText(msg)
        progressDialog!!.show()
    }

    open fun cancleProgress() {
        if (progressDialog != null && progressDialog!!.isShow) {
            progressDialog!!.cancel()
        }
    }

    /*******************  以下拓展方法  **************/
    /**
     *  @time 2024/7/8  11:12
     *  @author Im_jingwei
     *  @desc eventbuss订阅
     */
    fun registEventBus() {
        EventBus.getDefault().register(mContext)
    }

    /**
     *  @time 2024/7/8  11:12
     *  @author Im_jingwei
     *  @desc eventbuss解除订阅
     */
    fun unRegistEventBuss() {
        EventBus.getDefault().unregister(mContext)
    }

    /**
     *  @time 2024/7/8  11:13
     *  @author Im_jingwei
     *  @desc eventbuss发送数据
     */
    fun eventBusPost(event: Any?) {
        EventBus.getDefault().post(event)
    }

    /**
     *  @time 2025/1/13  9:28
     *  @author Im_jingwei
     *  @desc 是否是平板
     */
    fun isLand(): Boolean {
        val displayMetrics = resources.displayMetrics
        val screenWidth = displayMetrics.widthPixels
        val screenHeight = displayMetrics.widthPixels
        return screenWidth > screenHeight
    }

    fun setTextValue(textView: TextView, value: String?) {
        textView.text = if (isNull(value)) "" else value
    }

    fun setTextValue(textView: EditText, value: String?) {
        textView.setText(if (isNull(value)) "" else value)
    }

    // 在 Activity 或 Fragment 中调用
    fun hideKeyboard() {
        val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        // 第一个参数是当前窗口的 token
        imm.hideSoftInputFromWindow(window.decorView.windowToken, 0)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            finish()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}